#include <SPI.h>
#include "epd2in9.h"
#include "epdpaint.h"
#include "uTimerLib.h"

float HCHO;
unsigned char Re_buf[9],counter=0;
unsigned char sign=0;
unsigned char flag = 0 ;
float hcho;
byte Command[]=  {0xFF,0x01,0x78,0x40,0x00,0x00,0x00,0x00,0x47};//主动上传
byte Command1[]= {0xFF,0x01,0x78,0x41,0x00,0x00,0x00,0x00,0x46};//问答上传
byte Command2[]= {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};//问答读取

#define COLORED     0
#define UNCOLORED   1
Epd epd;
unsigned char image[1024];
Paint paint(image, 0, 0);    //width should be the multiple of 8 
int Width = 210;
int Height = 60;
char dispaly_[10]={'0','1','2','3','4','5','6','7','8','9'};
int yuan1;
double H;


void onDispaly(){
  flag = 1; 
}

void setup() {
  Serial.begin(9600);  
  //int out = 0;
 if (epd.Init(lut_full_update) != 0) {
      Serial.print("e-Paper init failed");
      return;
  }
  epd.ClearFrameMemory(0xFF);   // bit set = white, bit reset = black
  epd.DisplayFrame();
  epd.ClearFrameMemory(0xFF);   // bit set = white, bit reset = black
  epd.DisplayFrame();
 //TimerLib.setInterval_s(onDispaly, 10);

 if (epd.Init(lut_partial_update) != 0) {
      Serial.print("e-Paper init failed");
      return;
  }
}

void loop() {
  serialEvent();  
  if(sign)
  {  
     sign=0;
     char check=0;    
     for (int i=1;i<=7;i++)
     {
          check += Re_buf[i];
      }
          check =(~check)+1;                                                     //求和校验
      if(check == Re_buf[8])
      {                              
            if(Re_buf[0]==0xFF && Re_buf[1]==0x17 && Re_buf[2]==0x04 )        //检查帧头，帧尾
            {           
                  HCHO = Re_buf[4]*256+Re_buf[5];  
                  hcho = HCHO/1000*1.295;
                  Serial.println(hcho,6); 
                  //delay(200); 
            }
       } 
    }
    //hcho=10000;
    yuan1 = hcho*10000;
  H = (yuan1*0.0001);
  int G = H /1 ;
  int S = ((H-G)*10)/1;
  int B = ((H-G)*10-S)*10/1;
  int Q = (((H-G)*10-S)*10-B)*10/1;
  int W = ((((H-G)*10-S)*10-B)*10-Q)*10;
    char time_string[] = {'H','C','H','O',':', '8', '.','0','0', '0', '0','M','G','/','M','3', '\0'};
    time_string[5] = G + '0';
  time_string[7] = S + '0';
  time_string[8] = B + '0';
  time_string[9] = Q + '0';
  time_string[10] = W + '0';

  paint.SetWidth(11);
  paint.SetHeight(176);
  paint.SetRotate(ROTATE_90);

  paint.Clear(UNCOLORED);
  paint.DrawStringAt(0, 4, time_string, &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), 60, 57, paint.GetWidth(), paint.GetHeight());
  epd.DisplayFrame();
  //epd.Sleep();
  //delay(500);
       
       /*if(flag)
     {
      lnk_display();
      flag = 0;
     } */ 
}
void serialEvent() 
{
  while (Serial.available()>0) 
  {   
    Re_buf[counter]=(unsigned char)Serial.read();
    if(counter==0&&Re_buf[0]!=0xFF) return;      // 检查帧头         
    counter++;       
    if(counter==9)                //接收到数据
    {    
       counter=0;                 //重新赋值，准备下一帧数据的接收 
       sign=1;
    }      
  }
}
/*void lnk_display(){
  //epd.Init();
  //epd.Reset();
  paint.SetWidth(32);
  paint.SetHeight(96);
  paint.SetRotate(3);
  //epd.ClearFrame();
  char NAME[] = {'H','C', 'H', 'O', ':', '\0'};
  /*paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, "H", &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), Height, Width, paint.GetWidth(), paint.GetHeight());  //24 ,32
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, "C", &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), Height, Width-12*1, paint.GetWidth(), paint.GetHeight());  //24 ,32
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, "H", &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), Height, Width-12*2, paint.GetWidth(), paint.GetHeight());  //24 ,32
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, "O", &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), Height, Width-12*3, paint.GetWidth(), paint.GetHeight());  //24 ,32
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, ":", &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), Height, Width-12*4, paint.GetWidth(), paint.GetHeight());  //24 ,32
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, NAME, &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), Height, Width-12*1, paint.GetWidth(), paint.GetHeight());  //24 ,32
  int mid = 1;
  yuan1 = hcho*10000;
  H = (yuan1*0.0001);
  int G = H /1 ;
  int S = ((H-G)*10)/1;
  int B = ((H-G)*10-S)*10/1;
  int Q = (((H-G)*10-S)*10-B)*10/1;
  int W = ((((H-G)*10-S)*10-B)*10-Q)*10;
  char HCHO_string[] = {'0','.', '0', '0', '0', '0','M','G','/','M','3', '\0'};
  HCHO_string[0] = G + '0';
  HCHO_string[2] = S + '0';
  HCHO_string[3] = B + '0';
  HCHO_string[4] = Q + '0';
  HCHO_string[5] = W + '0';
  

  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, HCHO_string, &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), 60, Width-12*5+mid*3, paint.GetWidth(), paint.GetHeight()); //0,70
  /*paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, ".", &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), 60, Width-12*6+mid*4, paint.GetWidth(), paint.GetHeight()); //0,70
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, &dispaly_[S], &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), 60, Width-12*7+mid*5, paint.GetWidth(), paint.GetHeight()); //0,70
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1,  &dispaly_[B],&Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), 60, Width-12*8+mid*6, paint.GetWidth(), paint.GetHeight()); //0,70
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, &dispaly_[Q],&Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), 60, Width-12*9+mid*7, paint.GetWidth(), paint.GetHeight()); //0,70
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, &dispaly_[W],&Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), 60, Width-12*10+mid*8, paint.GetWidth(), paint.GetHeight()); //0,70
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, "M", &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), 60, Width-12*11+mid*9, paint.GetWidth(), paint.GetHeight()); //0,70
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, "G",&Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), 60, Width-12*12+mid*10, paint.GetWidth(), paint.GetHeight()); //0,70
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, "/", &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), 60, Width-12*13+mid*11, paint.GetWidth(), paint.GetHeight()); //0,70
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, "M", &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), 60, Width-12*14+mid*12, paint.GetWidth(), paint.GetHeight()); //0,70
  paint.Clear(UNCOLORED);
  paint.DrawStringAt(1, 1, "3", &Font16, COLORED);
  epd.SetFrameMemory(paint.GetImage(), 60, Width-12*15+mid*13, paint.GetWidth(), paint.GetHeight()); //0,70
  epd.DisplayFrame();
 // epd.Sleep();
}*/

